package cn.sh.yhk.vo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.sql.Timestamp;
import java.util.Date;

public class BasicVo {

    private static Date CURRENTTIME = new Timestamp(System.currentTimeMillis());
    private String message;
    private Boolean success;
    private Object object;
    private Long timestamp = CURRENTTIME.getTime();

    private BasicVo(String message, Object object) {
        this.message = message;
        this.object = object;
        this.timestamp = timestamp;
    }

    public BasicVo(Boolean success, Object object) {
        this.success = success;
        this.object = object;
    }

    public BasicVo(Boolean success, String message) {
        this.message = message;
        this.success = success;
    }

    public BasicVo(String message) {
        this.message = message;
    }

    public BasicVo(Boolean success, String message, Object object) {
        this.message = message;
        this.success = success;
        this.object = object;
    }

    public static void main(String[] args) {
        String storepass = "888999";
        try {
            FileInputStream fis = null;
            // 读取keystore文件转换为keystore密钥库对象
            fis = new FileInputStream("/Users/adminyhk/Desktop/adminyhk.keystore");
            // 因为生成证书的类型为JKS 也有其他的格式
            KeyStore keyStore = KeyStore.getInstance("jceks");
            // 该密钥库的密码"888999",storepass指定密钥库的密码(获取keystore信息所需的密码)
            keyStore.load(fis, storepass.toCharArray());
            fis.close();
            String password = "decryp pwd";
            // 根据别名（alias=desPws）从证书（keyStore）获取密码并解密
            //keyStore.getKey返回与给定别名关联的密钥，并用给定密码来恢复它。
            Key key = keyStore.getKey("desPws", password.toCharArray());
            //key.getEncoded()返回基本编码格式的密钥，如果此密钥不支持编码，则返回 null。
            System.out.println("从证书中获取的解密密码是：" + new String(key.getEncoded()));
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        }
    }

    public static BasicVo success(String message, Object object) {
        return new BasicVo(true, message, object);
    }

    public static BasicVo success(Object object) {
        return new BasicVo(true, object);
    }


    public static BasicVo success(String message) {
        return new BasicVo(true, message);
    }

    public static BasicVo success(Boolean success, Object object) {
        return new BasicVo(success, object);
    }

    public static BasicVo fail(String message) {
        return new BasicVo(false, message);
    }

    public static Date getCURRENTTIME() {
        return CURRENTTIME;
    }

    public static void setCURRENTTIME(Date CURRENTTIME) {
        BasicVo.CURRENTTIME = CURRENTTIME;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


}
